﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace FServer.Models
{
    public class NguoiDungDAO
    {
        private XmlDocument doc = new XmlDocument();

        private DataChatEntities data = new DataChatEntities();

        public void themNguoiDung(NguoiDung nd)
        {
            try
            {
                data.NguoiDungs.Add(nd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void xoaNguoiDung(String userName)
        {
            NguoiDung nd = data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == userName);
            data.NguoiDungs.Remove(nd);
            data.SaveChanges();
        }

        public NguoiDung getNguoiDungByName(String userName)
        {
            return data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == userName);
        }

        public NguoiDung dangNhap(String userName, String pass)
        {
            return data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == userName && a.MatKhau == pass);
        }

        public void capNhatNguoiDung(NguoiDung nd_new)
        {
            NguoiDung nd_old = data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == nd_new.TenDangNhap);
            nd_old = nd_new;
            data.SaveChanges();
        }

        public void themBanBe(String userName, String userFriendName) // user kết bạn với user friend ....add vào user
        {
            NguoiDung nd = data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == userName);

            XmlElement element;
            // thêm vào danh sách bạn bè
            doc.Load(nd.DanhSachBanBe);

            element = doc.DocumentElement;
            XmlElement adder = doc.CreateElement("friend");
            adder.SetAttribute("name", userFriendName);
            element.AppendChild(adder);
            nd.DanhSachBanBe = doc.OuterXml;

            // nếu tồn tại trong hàng đợi yêu cầu kết bạn thì hủy 
            doc.Load(nd.DanhSachYeuCauKetBan);
            element = doc.DocumentElement;
            XmlNode node = element.SelectSingleNode("//friendExpect[@name='"+ userFriendName +"']");

            if( node != null)
                node.ParentNode.RemoveChild(node);

            nd.DanhSachYeuCauKetBan = doc.OuterXml;

            data.SaveChanges();
        }

        public void themYeuCauKetBan(String userName,String userFriendName)
        {
            NguoiDung nd = data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == userFriendName);
            doc.Load(nd.DanhSachYeuCauKetBan);

            XmlElement element = doc.DocumentElement;
            XmlElement adder = doc.CreateElement("friendExpect");
            adder.SetAttribute("name", userName);
            element.AppendChild(adder);
            nd.DanhSachYeuCauKetBan = doc.OuterXml;
            data.SaveChanges();

        }

        public void huyKetBan(String userName, String userFriendName){
            NguoiDung nd = data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == userName);

            doc.Load(nd.DanhSachBanBe);

            XmlElement element = doc.DocumentElement;
            XmlNode node = element.SelectSingleNode("//friend[@name='"+ userFriendName+"']");
            node.ParentNode.RemoveChild(node);

            nd.DanhSachBanBe = doc.OuterXml;
            data.SaveChanges();

        }

        public void huyYeuCauKetBan(String userName, String userFriendName)
        {
            NguoiDung nd = data.NguoiDungs.FirstOrDefault(a => a.TenDangNhap == userName);

            doc.Load(nd.DanhSachYeuCauKetBan);

            XmlElement element = doc.DocumentElement;
            XmlNode node = element.SelectSingleNode("//friendExpect[@name='" + userFriendName + "']");

            node.ParentNode.RemoveChild(node);

            nd.DanhSachYeuCauKetBan = doc.OuterXml;
            data.SaveChanges();

        }
    }
}